package com.lgx.common.controller;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lgx.common.entity.Dictionary;
import com.lgx.common.service.IDictionaryService;
import com.lgx.common.web.json.JsonUtils;
import com.lgx.common.web.util.EscapeUtils;

@Controller
@RequestMapping("/dict")  
public class DictionaryController {

	private IDictionaryService dictionaryService;

	public IDictionaryService getDictionaryService() {
		return dictionaryService;
	}

	public void setDictionaryService(IDictionaryService dictionaryService) {
		this.dictionaryService = dictionaryService;
	}
	
	@RequestMapping("/dictMgr.do") 
	public String dictMgr(HttpServletRequest request,HttpServletResponse response){
		return "system/dict/dictMgr";
	}
	
	@RequestMapping("/list.do") 
	public void list(HttpServletRequest request,HttpServletResponse response){
		List<Dictionary> dicts = this.dictionaryService.findDictionaryList();
		
		JsonUtils utils = new JsonUtils();
		String json;
		try {
			json = utils.converToJson(dicts);
			response.setCharacterEncoding("utf-8");
			PrintWriter out = response.getWriter();
			out.print(json);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@RequestMapping("/init.do") 
	public String init(HttpServletRequest request,HttpServletResponse response){
		String type = request.getParameter("type");
		String dictId = request.getParameter("dictId");
		String kind = request.getParameter("kind");
		
		if(type.equals("edit")){
			Dictionary dict = this.dictionaryService.findDictionaryById(Long.parseLong(dictId));
			request.setAttribute("dict", dict);
		}
		
		request.setAttribute("type", type);
		request.setAttribute("dictId", dictId);
		request.setAttribute("kind", kind);
		
		return "system/dict/dict-init";
	}
	
	@RequestMapping("/save.do") 
	@ResponseBody
	public String save(HttpServletRequest request,HttpServletResponse response){
		String dictId = request.getParameter("dictId");
		Dictionary parent = this.dictionaryService.findDictionaryById(Long.parseLong(dictId));
		
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String kind = request.getParameter("kind");
		if(!StringUtils.isEmpty(kind))kind = EscapeUtils.unescape(kind);
		String code = request.getParameter("code");
		if(!StringUtils.isEmpty(code))code = EscapeUtils.unescape(code);
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(name))description = EscapeUtils.unescape(description);
		
		Dictionary dict = new Dictionary();
		dict.setName(name);
		dict.setCode(code);
		dict.setDescription(description);
		dict.setKind(kind);
		dict.setParent(parent);
		dict.setIsDelete("0");
		
		this.dictionaryService.createDictionary(dict);
		
		return "1";
	}
	
	@RequestMapping("/update.do") 
	@ResponseBody
	public String update(HttpServletRequest request,HttpServletResponse response){
		String dictId = request.getParameter("dictId");
		Dictionary dict = this.dictionaryService.findDictionaryById(Long.parseLong(dictId));
		
		String name = request.getParameter("name");
		if(!StringUtils.isEmpty(name))name = EscapeUtils.unescape(name);
		String kind = request.getParameter("kind");
		if(!StringUtils.isEmpty(kind))kind = EscapeUtils.unescape(kind);
		String code = request.getParameter("code");
		if(!StringUtils.isEmpty(code))code = EscapeUtils.unescape(code);
		String description = request.getParameter("description");
		if(!StringUtils.isEmpty(name))description = EscapeUtils.unescape(description);
		
		dict.setName(name);
		dict.setCode(code);
		dict.setDescription(description);
		dict.setKind(kind);
		
		this.dictionaryService.updateDictionary(dict);
		
		return "1";
	}
	
	@RequestMapping("/delete.do") 
	@ResponseBody
	public String delete(HttpServletRequest request,HttpServletResponse response){
		String dictId = request.getParameter("dictId");
		this.dictionaryService.deleteDictionary(Long.parseLong(dictId));
		return "1";
	}
}
